﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ESF.General;
using ESF.Web;
using ESF.General.Entity;
using ESF.SSO.Presenter.Admin.Menu;
using ESF.Web.Pages;
using ESF.SSO.Core.Entities.BasicReferences;
using ESF.SSO.Core.BusinessLogic;

#endregion


public partial class Menu_EditUserGroupMenu : BasePagePresenter<EditUserGroupMenuPresenter>, IEditUserGroupMenuViewer
{
    #region Carga Inicial
	
	protected void Page_Load(object sender, EventArgs e)
    {
		ScriptManager.GetCurrent (this).RegisterAsyncPostBackControl (uscGenericTreeView);

		_Presenter = new EditUserGroupMenuPresenter (this);

		if (!IsPostBack)
		{
			lblTitle.Text = Title;

			ListLoader.LoadList (ddlApplication, CtrlBasicReference.List (BasicReferenceTypes.Application));
			ListLoader.LoadList (ddlUserGroup, CtrlBasicReference.List (BasicReferenceTypes.UserGroup));
			
			_Presenter.Load ();

			Clear ();
		}
    }

	#endregion


	#region Funciones

	public void Clear ()
	{
		ddlApplication.Enabled = true;
		ddlUserGroup.Enabled = true;

		btnSave.Enabled = false;

		ddlApplication.SelectedIndex = -1;
		ddlUserGroup.SelectedIndex = -1;

		_Presenter.Clear ();
	}
	#endregion


	#region Campos del Interface

	public int? ApplicationID
	{
		get
		{
			return TextHelper.GetNullableInt (ddlApplication.SelectedValue);
		}
		set
		{
			ListLoader.Select (ddlApplication, value);
		}
	}


	public int? UserGroupID
	{
		get
		{
			return TextHelper.GetNullableInt (ddlUserGroup.SelectedValue);
		}
		set
		{
			ListLoader.Select (ddlUserGroup, value);
		}
	}


	public List<UserMenuItem> lstMainUserMenus
	{
		set
		{
			if (value == null)
			{
				uscGenericTreeView.RemoveAllNodes ();
				divEdition.Visible = false;
			}
			else
			{			
				uscGenericTreeView.SetRootNodes (value.ToArray ()); 
				divEdition.Visible = true;
			}
		}
	}


	public List<BasicReference> SelectedItems 
	{
		get
		{
			List<BasicReference> selectedItems;


			selectedItems = new List<BasicReference> ();
			
			foreach (Dictionary <string, string> selectedDatas in uscGenericTreeView.GetDatasForCheckedNodes ())
			{
				selectedItems.Add (
					new BasicReference () 
					{ 
						Code = selectedDatas ["Code"],
						BasicReferenceType = new BasicReferenceType () { ID = (int) BasicReferenceTypes.UserMenu }
					}
				);
			}

			return selectedItems;
		}
	}

	#endregion


	#region Eventos de los botones

	protected void btnClear_Click(object sender, EventArgs e)
	{
		Clear ();
	}

	protected void btnShowMenuForApp_Click(object sender, EventArgs e)
	{
		ddlApplication.Enabled = false;
		ddlUserGroup.Enabled = false;

		btnSave.Enabled = true;

		_Presenter.ShowMainUserMenus ();
	}


	protected void btnSave_Click(object sender, EventArgs e)
	{
		_Presenter.Save ();
	}

	#endregion


	#region Eventos del Arbol

	protected object[] uscGenericTreeView_SelectNodeGetChildren(Dictionary<string, string> pValueList, bool pFromExpand, int pDepth)
	{
		if (pFromExpand)
		{
			return _Presenter.GetUserMenusByLevel (TextHelper.GetNullableInt (pValueList ["ID"])).ToArray ();
		}

		return null;
	}
	
	#endregion
	
}